import {NgModule, ModuleWithProviders}    from '@angular/core';
import {CommonModule}                     from '@angular/common';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {Http, RequestOptions, XHRBackend} from "@angular/http";
import {RouterModule}         from "@angular/router";
import {InterceptorService}   from "ng2-interceptors";
import {UserLoginComponent}   from "./pages/user/user-login/user-login.component";
import {ServerURLInterceptor} from "./common/services/serverURLInterceptor";
import {AuthGuard}            from "./common/services/auth-guard";
import {NgZorroAntdModule, NzMessageService, NzNotificationService, NzModalService} from "ng-zorro-antd";
import {TopbarComponent}      from "./common/components/topbar/topbar.component";
import {SafeHtml}             from "./common/pipes/safe-html.pipe";
import {AmapComponent}        from "./common/components/amap/amap.component";
import {KeysPipe}             from "./common/pipes/keys.pipe";
import {IconsPipe}            from "./common/pipes/icons.pipe";

export function interceptorFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions, serverURLInterceptor: ServerURLInterceptor) {
  let service = new InterceptorService(xhrBackend, requestOptions);
  service.addInterceptor(serverURLInterceptor);
  return service;
}

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    RouterModule,
    NgZorroAntdModule
  ],
  declarations: [
    UserLoginComponent,
    TopbarComponent,
    AmapComponent,
    SafeHtml,
    KeysPipe,
    IconsPipe
  ],
  exports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    RouterModule,
    NgZorroAntdModule,
    UserLoginComponent,
    TopbarComponent,
    AmapComponent,
    SafeHtml,
    KeysPipe,
    IconsPipe
  ],
  providers: [
    AuthGuard,
    NzNotificationService,
    NzMessageService,
    NzModalService,
    ServerURLInterceptor,
    {
      provide: Http,
      useFactory: interceptorFactory,
      deps: [XHRBackend, RequestOptions, ServerURLInterceptor]
    }
  ]
})

export class SharedModule {
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: SharedModule,
      providers: []
    };
  }
}
